//
//  YHPStatus.swift
//  SinaSwift
//
//  Created by Yan on 16/3/14.
//  Copyright © 2016年 baidu. All rights reserved.
//

import UIKit
import AFNetworking

class YHPStatus: NSObject {

    //MARK: 属性
    var created_at: String?
    var id: Int64 = 0
    var text: String?
    var source: String?
    var user: YHPUser?
    var retweeted_status: YHPStatus?
    var imageURLs: [NSURL]?
    //获取配图数据 配图视图是一个数组
    var pic_urls: [[String : String]]? {
        didSet {
            //赋值
            guard let urls = pic_urls else {
                return
            }
            //实例化属性
            imageURLs = [NSURL]()
            for item in urls {
                let urlString = item["thumbnail_pic"]
                let url = NSURL(string: urlString!)!
                imageURLs?.append(url)
            }
        }
    }
    
    //INITMethod
    init(dict:[String: AnyObject]) {
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            guard let dict = value as? [String : AnyObject] else {
                return
            }
            user = YHPUser(dict: dict)
            return
        }
        if key == "retweeted_status" {
            guard let dict = value as? [String: AnyObject] else {
                return
            }
            retweeted_status = YHPStatus(dict:dict)
            return
        }
        super.setValue(value, forKey: key)
    }
 
    //MARK: 过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
   
    //MARK: 重写description属性
    override var description: String {
        let keys = ["id","text","source","created_at"]
        let dict = self.dictionaryWithValuesForKeys(keys)
        return dict.description
    }
}

//网络访问
//tableView 需要 模型数组数据  字典转模型
extension YHPStatus {
    class func loadData (since_id: Int64, finished:(array:[YHPStatus]?)->()) {
        
        let urlString: String = "https://api.weibo.com/2/statuses/home_timeline.json"
    
        guard let token = YHPUserAccount.shareAccount.access_token else {
            print("please login")
            return
        }
        var parameters = ["access_token" : token]
        
        //如果since_id > 0 就把since_id添加到参数中
        
        if since_id > 0 {
            parameters["since_id"] =  "/(since_id)"
        }
        let AFN = AFHTTPSessionManager()
        AFN.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            //打印返回数据
            //print(result)
            //判断数据是否为字典类型 否则返回
            guard let dict = result as? [String: AnyObject] else {
                print("数据结构错误")
                return
            }
            //判断数据是否为字典数组类型
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
                print("数据结构错误啦")
                return
            }
            //遍历数组 准备字典转模型
            var tempArray = [YHPStatus]()
            for item in array {
                let statu = YHPStatus(dict: item)
                tempArray.append(statu)
            }
            
            finished(array: tempArray)
            }) { (_, error) -> Void in
                print(error)
                finished(array: nil)
        }
    }
}


